package com.jt.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.OrderCommentMapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderComment;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;
import com.jt.service.DubboOrderService;
import com.jt.utils.Assert;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author dc
 */
@Service
public class OrderServiceImpl implements DubboOrderService {

	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;
	@Autowired
	private OrderCommentMapper orderCommentMapper;
	@Autowired
	AmqpTemplate template;

	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public Order saveOrder(Order order) {
		String orderId = ""+order.getUserId()+System.currentTimeMillis();
		Date date = new Date();
		//创建订单
		order.setOrderId(orderId).setStatus(5).setPaymentTime(date).setPostFee("京淘包邮").setShippingName("京淘快递").setCreated(date).setUpdated(date);
		//2.订单上传至消息队列
		template.convertAndSend("orderQueue",order);
		return order;
	}

	@Transactional(rollbackFor = RuntimeException.class)
	@Override
	public Order findOrderById(String id) {
		Order order = orderMapper.selectById(id);
		OrderShipping orderShipping = orderShippingMapper.selectById(id);
		QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("order_id",id);
		List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
		return order.setOrderShipping(orderShipping).setOrderItems(orderItems);
	}

	@Override
	public IPage<Order> findOrderByUserId(Long userId,String keyword,Integer page, Integer rows) {
		//1.根据用户id查询所有订单
		IPage<Order> iPage = new Page<>(page,rows);
		QueryWrapper<Order> userQuery = new QueryWrapper<>();
		userQuery.eq("user_id",userId)
				.like(keyword!=null&&!"".equals(keyword),"order_id", keyword);
		IPage<Order> orderPage = orderMapper.selectPage(iPage,userQuery);
		//2.循环每笔订单
		for (Order order:orderPage.getRecords()){
			//3.根据订单id查询每笔订单下的商品信息
			String orderId=order.getOrderId();
			QueryWrapper<OrderItem> itemQuery = new QueryWrapper<>();
			itemQuery.eq("order_id",orderId);
			List<OrderItem> items = orderItemMapper.selectList(itemQuery);
			//4.遍历商品，去查询评论表是否已评论
			for(OrderItem item:items){
				QueryWrapper<OrderComment> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("item_id",item.getItemId()).eq("order_id",item.getOrderId());
				OrderComment row = orderCommentMapper.selectOne(queryWrapper);
				if(row!=null){
					item.setFlag(1);
				}
			}
			//5.根据每笔订单id查询物流信息
			order.setOrderItems(items);
			QueryWrapper<OrderShipping> shipQuery = new QueryWrapper<>();
			shipQuery.eq("order_id",orderId);
			OrderShipping orderShipping = orderShippingMapper.selectById(orderId);
			order.setOrderShipping(orderShipping);
		}
		return orderPage;
	}

	@Override
	public OrderItem findItemByItemId(String itemId,String orderId) {
		Assert.isValid(itemId==null,"订单评价拉取商品信息出错，请稍后再试");
		QueryWrapper<OrderItem> queryWrapper=new QueryWrapper();
		queryWrapper.eq("item_id",itemId).eq("order_id",orderId);
		OrderItem item = orderItemMapper.selectOne(queryWrapper);
		return item;
	}

	@Override
	public void saveOrderComment(OrderComment orderComment) {
		Assert.isValid(orderComment==null,"评论走丢了，请稍后再试");
		String s = UUID.randomUUID().toString();
		String replace = s.replace("-", "").toUpperCase();
		orderComment.setId(replace);
		int row = orderCommentMapper.insert(orderComment);
		Assert.isServiceValid(row==0,"网络故障，请稍后再试");
	}

	@Override
	public OrderComment findOrderComment(String itemId, String orderId) {
		Assert.isValid(itemId==null,"商品走丢了。。。");
		Assert.isValid(orderId==null,"订单走丢了。。。");
		QueryWrapper<OrderComment> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("item_id",itemId).eq("order_id",orderId);
		OrderComment comment = orderCommentMapper.selectOne(queryWrapper);
		return comment;
	}
}
